<template>
  <el-container>
    <el-header>
      <el-input v-model="input" placeholder="输入关键词" />
      <el-select v-model="selectVal" class="select-btn" placeholder="请求类型">
        <el-option
          v-for="item in FormData.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button :icon="Search" type="primary" class="search-btn" />
    </el-header>
    <el-main>
      <el-table :data="FormData.tableData" style="width: 100%">
        <el-table-column prop="url" label="url"> </el-table-column>
        <el-table-column prop="method" label="method"> </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-footer>
  </el-container>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from "vue"
import { getInterfaceList } from "../../request/login-view/login"
import { Search } from "@element-plus/icons-vue"
import { log } from "console"

const currentPage = ref(0)
const input = ref()
const selectVal = ref()
const pageSize = ref(10)
const total = ref()

let FormData = reactive({
  options: [],
  tableData: [],
})

// 设置每页几条数据
const handleSizeChange = (val: any) => {
  pageSize.value = val
  getTableData()
  console.log(`每页 ${val} 条`)
}

// 翻页后获取当前页
const handleCurrentChange = (val: any) => {
  currentPage.value = val
  getTableData()
  console.log(`当前页: ${val}`)
}

// 获取当前表格数据
const getTableData = () => {
  getInterfaceList({
    current: currentPage.value,
    pageSize: pageSize.value,
  }).then((res) => {
    total.value = res.data.data.total
    FormData.tableData = []
    res.data.data.records.map((e: never) => {
      FormData.tableData.push(e)
    })
    let newArr = []
    FormData.tableData.forEach((e, i) => {
      if (newArr.indexOf(e.method) == -1) {
        newArr.push(e.method)
      }
    })
    FormData.options = []
    newArr.forEach((e) => {
      FormData.options.push({
        value: e,
        label: e,
      })
    })
  })
}

onMounted(() => {
  getTableData()
})
</script>

<style lang="scss" scoped>
.container {
  .el-input {
    width: 200px;
  }
  .search-btn {
    margin-left: 8px;
  }
  .select-btn {
    margin-left: 8px;
  }
}
</style>
